package lib8;

public class SparseMatrix {

    public TripleNode data[];
    public int rows;
    public int cols;
    public int nums;


    public SparseMatrix( int maxSize){
        data = new TripleNode[maxSize];
        for (int i = 0; i < data.length; i++) {
            data[i] = new TripleNode();
        }
        rows = 0;
        cols = 0;
        nums = 0;
    }
    public void printMaterix () {
        int i;
        System.out.println("稀疏矩阵的三元组存储结构为：");
        System.out.println("行数：" + rows + ",列数：" + cols + ",非零元素个数：" + nums);

        for (i = 0; i < nums; i++) {
            System.out.println(data[i].row + "\t" + data[i].column + "\t" + data[i].value);
        }

    }//三元组表的打印
    public void output0() {
        int[][] n = new int[rows][cols];
        for (int i= 0; i< nums; i++) {
            n[data[i].row][data[i].column] = data[i].value;
        }
        System.out.println("稀疏矩阵为：");
        for (int a = 0; a < rows; a++) {
            for (int b = 0; b < cols; b++) {
                System.out.print(n[a][b] + "  ");
            }

            System.out.println("");
        }
    }//用三元组表输出矩阵


    public SparseMatrix( int mat[][]){
        int i, j, k = 0, count = 0;
        rows = mat.length;
        cols = mat[0].length;
        for (i = 0; i < mat.length; i++) {
            for (j = 0; j < mat[i].length; j++) {
                if (mat[i][j] != 0) {
                    count++;
                }
            }
        }
        nums = count;
        data = new TripleNode[nums];
        for (i = 0; i < mat.length; i++) {
            for (j = 0; j < mat[i].length; j++) {
                if (mat[i][j] != 0) {
                    data[k] = new TripleNode(i, j, mat[i][j]);
                    k++;
                }
            }
        }//三元组表的建立

    }
    public SparseMatrix fasttranspose(){
        SparseMatrix tm=new SparseMatrix(nums);
        tm.cols=rows;
        tm.rows=cols;
        tm.nums=nums;
        int i,j=0,k=0;
        int[] num,copt;
        if(nums>0){
            num=new int[cols];
            copt=new int[rows];
            for (i=0;i<cols;i++){
                num[i]=0;
            }
            for (i=0;i<nums;i++){
                j=data[i].column;
                num[j]++;
            }
            copt[0]=0;
            for (i=1;i<cols;i++){
                copt[i]=copt[i-1]+num[i-1];
            }
            for (i=0;i<nums;i++){
                j=data[i].column;
                k=copt[j];
                tm.data[k].row=data[i].column;
                tm.data[k].column=data[i].row;
                tm.data[k].value=data[i].value;
                copt[j]++;
            }
        }
        return tm;
    }//矩阵的快速转置
    public void output1() {
        int[][] n = new int[rows][cols];
        for (int i= 0; i< nums; i++) {
            n[data[i].row][data[i].column] = data[i].value;
        }
        System.out.println("转置后稀疏矩阵为：");
        for (int a = 0; a < rows; a++) {
            for (int b = 0; b < cols; b++) {
                System.out.print(n[a][b] + "  ");
            }

            System.out.println("");
        }//矩阵转置的打印
    }



    public void output3() {
        int[][] n = new int[rows][cols];
        for (int i= 0; i< nums+1; i++) {
            n[data[i].row][data[i].column] = data[i].value;
        }
        System.out.println("稀疏矩阵为：");
        for (int a = 0; a < rows; a++) {
            for (int b = 0; b < cols; b++) {
                System.out.print(n[a][b] + "  ");
            }

            System.out.println("");
        }
    }//用三元组表输出矩阵

    public static SparseMatrix Add(SparseMatrix a, SparseMatrix b) {
        if (a.rows != b.rows || a.cols != b.cols) {
            System.out.println("两矩阵不能相加");
            return null;

        }
        SparseMatrix c=new SparseMatrix(a.nums+b.nums);
        int i=0,j=0,k=0;
        int len=0;
        c.cols=a.cols;
        c.rows=a.rows;
        while (i<a.nums&&j<b.nums){
            if(a.data[i].row<b.data[j].row){
                c.data[k].column=a.data[i].column;
                c.data[k].row=a.data[i].row;
                c.data[k].value=a.data[i].value;
                c.nums=++k;
                i++;
            }
            else if(a.data[i].row==b.data[j].row){
                if(a.data[i].column==b.data[j].column){
                    if(a.data[i].value+b.data[j].value!=0){
                        c.data[k].column=a.data[i].column;
                        c.data[k].row=a.data[i].row;
                        c.data[k].value=a.data[i].value+b.data[j].value;
                        c.nums=++k;

                    }
                    i++;
                    j++;
                } else if (a.data[i].column<b.data[j].column) {
                    c.data[k].column=a.data[i].column;
                    c.data[k].row=a.data[i].row;
                    c.data[k].value=a.data[i].value;
                    c.nums=++k;
                    i++;
                } else if (a.data[i].column>b.data[j].column) {
                    c.data[k].column=b.data[j].column;
                    c.data[k].row=b.data[j].row;
                    c.data[k].value=b.data[j].value;
                    c.nums=++k;
                    j++;
                } else if (a.data[i].row>b.data[j].row) {
                    c.data[k].column=b.data[j].column;
                    c.data[k].row=b.data[j].row;
                    c.data[k].value=b.data[j].value;
                    c.nums=++k;
                    j++;
                }
            }
            while (i<a.nums){
                c.data[k].column=a.data[i].column;
                c.data[k].row=a.data[i].row;
                c.data[k].value=a.data[i].value;
                c.nums=++k;
                i++;
            }
            while (j< b.nums){
                c.data[k].column=b.data[j].column;
                c.data[k].row=b.data[j].row;
                c.data[k].value=b.data[j].value;
                c.nums=++k;
                j++;
            }
            c.printMaterix();
            c.output0();
        }
        return c;
    }
    public static SparseMatrix Multiple(SparseMatrix a,SparseMatrix b) {
        if (a.cols != b.rows) {
            System.out.println("两矩阵不能相加");
            return null;
        }
        SparseMatrix c=new SparseMatrix(a.nums+b.nums);
        int i=0,j=0,k=0;
        int len=0;
        c.cols=a.cols;
        c.rows=a.rows;
        return c;
    }
}


